package com.isoftstone.merchant.service.accountCheck;

import com.isoftstone.merchant.dao.payment.PaymentMapper;
import com.isoftstone.merchant.entity.payment.Payment;
import com.isoftstone.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import sun.net.ftp.FtpClient;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhaojg
 * @Description
 * @Date: Created in 11:19 2018/1/8
 * @Modified by:
 */

@Component("task")
@Service
public class accountCheckService {

    @Autowired
    PaymentMapper paymentMapper;

    protected Logger logger = Logger.getLogger(this.getClass());

    //从ftp上下载文件
    @Scheduled(cron = "0/2000 * * * * ? ")
    public void getFtp() {

        //ftp服务器IP
        String middle_ftpServer = "127.0.0.1";
        //ftp服务器端口
        Integer middle_port = 21;
        //ftp服务器用户名
        String middle_user = "host";
        //ftp服务器密码
        String middle_password = "123456";

        //连接ftp服务器
        FtpClient ftp = SunFtpUtil.getSunFtpUtil().connectFTP(middle_ftpServer, middle_port, middle_user, middle_password);

        //文件名称
        String wantFileName = DateUtil.getDays() + ".txt";


        File file = new File("E:\\ftp下载文件夹\\");

        if (!file.exists()) {
            file.mkdirs();
        }
        //定义本地路径
        String localFile = file + File.separator + wantFileName;

        //从ftp上下载文件
        boolean resultFile = SunFtpUtil.getSunFtpUtil().download(localFile, wantFileName, ftp);

        if (resultFile) {

            //读取文件，放入集合
            String fileName = wantFileName;
            file = new File(localFile);
            List<String> stringList = new ArrayList<String>();
            try {
                FileInputStream fis = new FileInputStream(file);
                DataInputStream in = new DataInputStream(fis);
                BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
                String str = null;
                while ((str = br.readLine()) != null) {
                    stringList.add(str);
                    System.out.println(str);
                }
                br.close();

                Map<String, String> cspRetMap = new HashMap<>();
                List<String> cspList = new ArrayList<String>();
                //获取每一行的数据
                for (String listLine : stringList) {
                    System.out.println("listLine = " + listLine);
                    logger.info("=============csp对账文件返回文件的每行数据============" + listLine);
                    if (!StringUtils.equals(listLine, "")) {
                        String[] cspSplit = listLine.split("\\|");
                        if (cspSplit.length > 0) {
                            //快缴流水号
                            String cspPayId = cspSplit[4];
                            System.out.println("cspPayId = " + cspPayId);
                            logger.info("=============csp对账文件返回的payId============" + cspPayId);
                            //金额
                            String cspFactAmt = cspSplit[7];
                            System.out.println("cspFactAmt = " + cspFactAmt);
                            logger.info("=============csp对账文件返回的实缴金额============" + cspFactAmt);
                            //商户号
                            String cspCorpCode = cspSplit[8];
                            System.out.println("cspCorpCode = " + cspCorpCode);
                            logger.info("=============csp对账文件返回的商户号============" + cspCorpCode);

                            //csp返回的都是支付成功的报文数据
                            //根据上面cspPayId,factAmt参数去m_payment_main表中查询，对比该条记录的payStatus的状态是否为"P-00"
                            Payment payment = paymentMapper.queryPaymentByPayIdAndFactAmt(Long.parseLong(cspPayId), cspFactAmt);
                            if (payment != null) {
                                //payStatus
                                String payStatus = payment.getPayStatus();
                                if (payStatus != null && StringUtils.equals(payStatus, Const.DJF)) {
                                    //根据cspPayId修改payStatus的状态和对账状态
                                    paymentMapper.updatePayment(payment.getPayId());
                                } else {
                                    //根据cspPayId更新对账状态为1
                                    paymentMapper.updatePaymentForIsSelect(payment.getPayId());
                                }
                            }
                        }
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }


    }


}
